package cn.chinasyq.photoquan;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;

import java.util.List;

import cn.chinasyq.photoquan.fragment.PreviewNetWorkPictureFragment;
import cn.chinasyq.photoquan.photo.adapter.PhotoPagerTabAdapter;
import cn.chinasyq.photoquan.util.BitmapUtil;
import cn.master.util.utils.ToastUtil;
import cn.master.volley.commons.VolleyHelper;

public class PreviewNetworkPhotoActivity extends FragmentActivity implements View.OnClickListener {
    private ViewPager pager;
    private PhotoPagerTabAdapter adapter;
    private List<String> image_url;
    private TextView tv_title;
    private int position;
    public static final int MODE_EDIT = 1;
    public static final int MODE_PREVIEW = 0;
    public static final String MODE = "mode";
    private int mode;
    public static final String INTENT_FILE_PATH = "images";
    public static final String INTENT_POSITION = "position";
    private Button btn_right;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview_network_picture);
        pager = (ViewPager) findViewById(R.id.gallery);
        image_url = getIntent().getStringArrayListExtra(INTENT_FILE_PATH);
        position = getIntent().getIntExtra(INTENT_POSITION, 0);
        adapter = new PhotoPagerTabAdapter(this, pager);
        mode = getIntent().getIntExtra(MODE, MODE_PREVIEW);
        for (int i = 0; i < image_url.size(); i++) {
            Bundle bundle = new Bundle();
            bundle.putString("url", image_url.get(i));
            adapter.addItemInfo(0x100 + i, "",
                    PreviewNetWorkPictureFragment.class, bundle);
        }
        pager.setOffscreenPageLimit(0);
        tv_title = (TextView) findViewById(R.id.tv_title);
        btn_right = (Button) findViewById(R.id.btn_right);
        if (mode == MODE_EDIT) {
            btn_right.setText("删除");
            btn_right.setVisibility(View.VISIBLE);
        } else {
            btn_right.setVisibility(View.GONE);
        }
        if (image_url.size() < 2) {
            tv_title.setText((position + 1) + "/" + image_url.size());
        } else {
            tv_title.setText("");
        }
        pager.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageSelected(int arg0) {
                position = arg0;
                tv_title.setText((arg0 + 1) + "/" + image_url.size());
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
        pager.setCurrentItem(position);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ibtn_left:
                finish();
                break;
            /** 黑色viewpager模板 */
            case R.id.btn_right:
              if (mode==MODE_EDIT){
                  setResult(RESULT_OK);
                  finish();
              }else {

              }
                break;
            default:
                break;
        }

    }

    private void saveImage() {
        if (image_url == null || image_url.size() == 0) {
            return;
        }
        String url = image_url.get(position);
        VolleyHelper.getImageLoader().get(url, new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                if (response != null && response.getBitmap() != null) {
                    BitmapUtil.saveBitmapToFile(response.getBitmap(), getString(R.string.app_name)
                            + System.currentTimeMillis() + ".jpg");
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                ToastUtil.show(PreviewNetworkPhotoActivity.this, getString(R.string.failedtosavephoto), ToastUtil.LENGTH_SHORT);
            }
        });
    }

}
